<template>
  <el-container>
    <!-- 头部区域 -->
    <el-header style="height:auto;">
      <div class="left">
        <!-- logo -->
        <span class="logo"></span>
        <!-- 顶部标题 -->
        <span class="words">龙之谷后台管理系统</span>
      </div>
      <div class="right">
        <el-button type="info" @click="logout">退出</el-button>
      </div>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px':'200px'">
        <div class="toggle-collapse" @click="isCollapse = !isCollapse">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409Eff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activaPath"
        >
          <!-- 一级导航 -->
          <el-submenu :index="'/' + item.path" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="savaNavState('/' + subItem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体结构 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      menuList: [],
      iconsObj: {
        125: 'iconfont el-icon-s-custom',
        103: 'iconfont el-icon-copy-document',
        101: 'iconfont el-icon-s-goods',
        102: 'iconfont el-icon-s-order',
        145: 'iconfont el-icon-data-line'
      },
      isCollapse: false,
      activaPath: ''
    }
  },
  created () {
    this.getMenuList()
    this.activaPath = window.sessionStorage.getItem('activePath')
  },
  mounted () {},
  methods: {
    logout () {
      this.$message({
        message: '退出成功',
        type: 'success',
        duration: 1000
      })
      window.sessionStorage.clear('token')
      this.$router.push('/login')
    },
    async getMenuList () {
      const res = await this.$http.get('menus')
      if (res.meta.status !== 200) {
        this.$message({
          message: res.meta.msg,
          type: 'error',
          duration: 1000
        })
      } else {
        this.menuList = res.data
      }
    },
    savaNavState (active) {
      window.sessionStorage.setItem('activePath', active)
      this.activePath = active
    }
  }
}
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
  color: #fff;
}
.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #a7c5ff;

  .left {
    .logo {
      display: inline-block;
      width: 190px;
      height: 78px;
      vertical-align: bottom;
      background: url(../assets/logo.png) no-repeat center;
    }
    .words {
      font-size: 24px;
      line-height: 72px;
    }
  }

  .right {
    .el-butto {
      vertical-align: bottom;
    }
  }
}

.toggle-collapse {
  width: 100%;
  height: 30px;
  line-height: 30px;
  text-align: center;
  font-size: 14px;
  cursor: pointer;
  letter-spacing: 0.2em;
  background-color: #365ca7;
}
.el-aside {
  background-color: #333744;
  line-height: 200px;
}

.el-menu {
  border-right: none;
}

.iconfont {
  font-size: 18px;
  margin-right: 10px;
}
.el-main {
  background-color: #eaedf1;
  color: #333;
}
</style>
